package com.woniuxy.springparking.service.Impl;

import com.woniuxy.springparking.dao.PermissionDao;
import com.woniuxy.springparking.dao.UserDao;
import com.woniuxy.springparking.domain.TreeNode;
import com.woniuxy.springparking.pojo.Permission;
import com.woniuxy.springparking.pojo.User;
import com.woniuxy.springparking.service.UserService;
import com.woniuxy.springparking.util.TreeNodeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private UserDao userDao;

    @Override
    public User login(String telephone, String password) {
        return userDao.selectUserByNameAndPwd(telephone,password);
    }

    @Override
    public List<TreeNode> selectMenusByUid(int uid) {
        List<TreeNode> treeNodes=new ArrayList<>();
      List<Permission> permissionList= this.permissionDao.selectMenusByUid(uid);
        for(Permission permission:permissionList){
            TreeNode treeNode=new TreeNode();
            treeNode.setId(permission.getId());
            treeNode.setPid(permission.getPid());
            treeNode.setTitle(permission.getName());
            treeNode.setIcon(permission.getIcon());
            treeNode.setHref(permission.getHref());
            treeNode.setSpread(permission.getOpen()=="1"?true:false);
            treeNodes.add(treeNode);
        }
        return TreeNodeBuilder.build(treeNodes,1);
    }

    @Override
    public void updataUserpsw(User user) {
        this.userDao.updataUserpsw(user);
    }

    @Override
    public User findUserByUserName(String telephone) {
        return this.userDao.queryUserByUserName(telephone);
    }

    @Override
    public String findReaNameByUserName(String username) {
        return this.userDao.seletreaNameByUserName(username);
    }
}
